package com._520it.wms.service.impl;

import com._520it.wms.domain.*;
import com._520it.wms.mapper.ProductStockMapper;
import com._520it.wms.mapper.SaleAccountMapper;
import com._520it.wms.mapper.StockOutcomeBillItemMapper;
import com._520it.wms.mapper.StockOutcomeBillMapper;
import com._520it.wms.query.PageResult;
import com._520it.wms.query.QueryObject;
import com._520it.wms.service.IProductStockService;
import com._520it.wms.service.IStockOutcomeBillService;
import com._520it.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by HASEE on 2017/11/14.
 */
@Service
public class StockOutcomeBillServiceImpl implements IStockOutcomeBillService {
    @Autowired
    private StockOutcomeBillMapper stockOutcomeBillMapper;
    @Autowired
    private IProductStockService productStockService;
    @Autowired
    private SaleAccountMapper saleAccountMapper;
    //注入订单明细
    @Autowired
    private StockOutcomeBillItemMapper stockOutcomeBillItemMapper;

    @Override
    public void saveOrUpdate(StockOutcomeBill stockOutcomeBill) {
        if (stockOutcomeBill.getId() != null) {
            //修改
            //删除原来的明细数据
            stockOutcomeBillItemMapper.deleteByBillId(stockOutcomeBill.getId());
            //计算总金额总数量
            BigDecimal totalNumber = BigDecimal.ZERO;
            BigDecimal totalAmount = BigDecimal.ZERO;
            //遍历明细
            for (StockOutcomeBillItem item : stockOutcomeBill.getItems()) {
                BigDecimal number = item.getNumber();
                BigDecimal salePrice = item.getSalePrice();
                BigDecimal amount = number.multiply(salePrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                item.setAmount(amount);
                totalAmount = totalAmount.add(amount);
                totalNumber = totalNumber.add(number);
                //设置关联关系 保存明细单
                item.setBillId(stockOutcomeBill.getId());
                stockOutcomeBillItemMapper.insert(item);
            }
            //出库单修改 编号,仓库,客户,时间 总数,总金额
            stockOutcomeBill.setTotalAmount(totalAmount);
            stockOutcomeBill.setTotalNumber(totalNumber);
            stockOutcomeBillMapper.updateByPrimaryKey(stockOutcomeBill);
        } else {
            //新增操作
            //设置制单人
            stockOutcomeBill.setInputUser(UserContext.getCurrentUser());
            //设置制单时间
            stockOutcomeBill.setInputTime(new Date());
            //设置状态
            stockOutcomeBill.setStatus(StockIncomeBill.NORMAL);
            //设置总金额总数量
            BigDecimal totalNumber = BigDecimal.ZERO;
            BigDecimal totalAmount = BigDecimal.ZERO;//总金额
            //遍历出价格和数量在明细表中
            for (StockOutcomeBillItem item : stockOutcomeBill.getItems()) {
                BigDecimal number = item.getNumber();
                BigDecimal salePrice = item.getSalePrice();
                BigDecimal amount = salePrice.multiply(number).setScale(2, BigDecimal.ROUND_HALF_UP);
                item.setAmount(amount);
                totalAmount = totalAmount.add(amount);
                totalNumber = totalNumber.add(number);
            }
            //把价格和数量设置进去
            stockOutcomeBill.setTotalAmount(totalAmount);
            stockOutcomeBill.setTotalNumber(totalNumber);
            //保存出库单
            stockOutcomeBillMapper.insert(stockOutcomeBill);
            //保存出库明细
            for (StockOutcomeBillItem item : stockOutcomeBill.getItems()) {
                //建立关联关系
                item.setBillId(stockOutcomeBill.getId());
                stockOutcomeBillItemMapper.insert(item);

            }
        }


    }

    @Override
    public void delete(Long id) { //查询主键id
        StockOutcomeBill stockOutcomeBill = stockOutcomeBillMapper.selectByPrimaryKey(id);
        //判断是否审核
        if (StockOutcomeBill.NORMAL == stockOutcomeBill.getStatus()) {
            //删除入库明细
            stockOutcomeBillItemMapper.deleteByBillId(id);
            //删除主键
            stockOutcomeBillMapper.deleteByPrimaryKey(id);
        }

    }

    @Override
    public StockOutcomeBill get(Long id) {
        return stockOutcomeBillMapper.selectByPrimaryKey(id);
    }


    @Override
    public PageResult query(QueryObject qo) {
        int totalCount = stockOutcomeBillMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getCurrentPage(), qo.getPageSize());
        }
        //查询结果集
        List<StockOutcomeBill> list = stockOutcomeBillMapper.queryForList(qo);
        return new PageResult(qo.getCurrentPage(), qo.getPageSize(), list, totalCount);
    }

    @Override
    //库存信息审核
    public void audit(Long id) {
        //1根据id找到对应的出库单
        StockOutcomeBill bill = stockOutcomeBillMapper.selectByPrimaryKey(id);
        //判断状态
        if (StockOutcomeBill.NORMAL == bill.getStatus()) {
            //设置审核人审核时间
            bill.setAuditTime(new Date());
            bill.setAuditor(UserContext.getCurrentUser());
            bill.setStatus(StockOutcomeBill.AUDIT);
            //更新出库订单状态 审核人时间 单据状态
            stockOutcomeBillMapper.audit(bill);
            //遍历商品明细,开始出库
            Depot depot = bill.getDepot();
            for (StockOutcomeBillItem item : bill.getItems()) {
                //出库操作在销售账单中插入对应的记录
                //得到一个成本价格
                BigDecimal costPrice = productStockService.outcome(item.getProduct(), depot, item.getNumber());
                SaleAccount saleAccount = new SaleAccount();
                saleAccount.setClient(bill.getClient());//客户
                saleAccount.setSaleman(bill.getInputUser());//销售人员
                saleAccount.setProduct(item.getProduct());//货品
                saleAccount.setVdate(bill.getVdate());//日期
                saleAccount.setNumber(item.getNumber()); //数量
                //销售总额=销售价格* 销售数量
                BigDecimal saleAmount = item.getNumber().multiply(item.getSalePrice()).setScale(2, BigDecimal.ROUND_HALF_UP);
                //成本总额=成本价格*数量
                BigDecimal costAmount = costPrice.multiply(item.getNumber()).setScale(2, BigDecimal.ROUND_HALF_UP);
                saleAccount.setSalePrice(costPrice); //成本价格
                saleAccount.setCostAmount(costAmount);//成本总金额
                saleAccount.setSalePrice(item.getSalePrice());//销售价格
                saleAccount.setSaleAmount(saleAmount);//销售总金额
                saleAccountMapper.insert(saleAccount);
            }
        } else {
            throw new RuntimeException("已经审核的单据不能重复审核");

        }
    }
}
